package summer.binarysearch;

public class Solution33 {
    // 注意题目条件 nums中不存在重复
    // 但要注意如果mid和i相比大小，还是可能存在相等的情况
    // https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/22068/ji-bai-liao-9983de-javayong-hu-by-reedfan
    // https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/solutions/22056/zai-javazhong-ji-bai-liao-100de-yong-hu-by-reedfan
    public int search(int[] nums, int target) {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) {
            int mid = (i + j) >> 1;
            if (nums[mid] == target) return mid;
            if (nums[mid] < nums[j]) {
                if (target > nums[mid] && target <= nums[j]) i = mid + 1;
                else j = mid - 1;
            } else {
                if (nums[mid] > target && target >= nums[i]) j = mid - 1;
                else i = mid + 1;
            }
        }
        return -1;
    }
}
